package com.heima.wemedia.listener;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.api.feign.ApArticleFeignClient;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.threadlocal.WmThreadLocalUtil;
import com.heima.wemedia.config.RabbitMqConfig;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description listerner
 * @Author dmx
 * @Date 2024-11-27
 */
@Component
public class SimpleListener {

@Autowired
private WmNewsMapper wmNewsMapper;

@Autowired
private WmUserMapper wmUserMapper;

@Autowired
private ApArticleFeignClient apArticleFeignClient;
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = RabbitMqConfig.DL_QUEUE),
            exchange = @Exchange(name = RabbitMqConfig.DL_EXCHANGE,
                    type = ExchangeTypes.DIRECT),
            key = RabbitMqConfig.DL_ROUTE_KEY))
    public void AuditListener(String msg){

        LambdaUpdateWrapper<WmNews> updateWrapper = Wrappers.lambdaUpdate();
        long id = Long.parseLong(msg);
        updateWrapper.eq(WmNews::getId,id);
        updateWrapper.set(WmNews::getStatus,(short) 9);
        //修改自媒体文章的状态
        wmNewsMapper.update(null,updateWrapper);
        //远程调用接口发布
        WmNews wmNews = wmNewsMapper.selectById(id);
        ArticleDto articleDto = BeanUtil.copyProperties(wmNews, ArticleDto.class);
        articleDto.setId(null);
        WmUser user = WmThreadLocalUtil.getUser();
        Integer authorId = user.getId();
        articleDto.setAuthorId(authorId.longValue());//设置作者id
        WmUser wmUser = wmUserMapper.selectById(authorId);
        articleDto.setAuthorName(wmUser.getName());//设置作者真实姓名
        apArticleFeignClient.save(articleDto);

    }

}
